package space.xxhui.principleof6.ImageCacheImp;

import android.content.Context;
import android.graphics.Bitmap;

import space.xxhui.principleof6.interfaces.IImageCache;

/**
 * Created by hui on 2016/10/21.
 */

public class DoubleImageLruCache implements IImageCache {
    private Context context;
    private IImageCache lruCahce;
    private IImageCache diskLruCahce ;

    private static DoubleImageLruCache instance;

    private DoubleImageLruCache(Context context) {
        this.context = context;
        lruCahce = ImageLruCache.getInstance(context);
        diskLruCahce = ImageDiskLruCache.getInstance(context);
    }

    public static DoubleImageLruCache getInstance(Context context) {
        if (instance == null) {
            synchronized (ImageDiskLruCache.class) {
                if (instance == null) {
                    instance = new DoubleImageLruCache(context);
                }
            }
        }
        return instance;
    }

    @Override
    public void setBitmap(String url, Bitmap bitmap) {
        lruCahce.setBitmap(url,bitmap);
        diskLruCahce.setBitmap(url,bitmap);
    }

    @Override
    public Bitmap getBitmap(String url) {
        Bitmap bitmap = lruCahce.getBitmap(url);
        if (bitmap==null){
            bitmap = diskLruCahce.getBitmap(url);
        }
        return bitmap;
    }
}
